package sng.modules.view.components.city.logic.unit
{
	import as3isolib.geom.Pt;
	import as3isolib.utils.IsoDrawingUtil;
	
	import com.tencent.animator.view.AnimatorCollection;
	import com.tencent.animator.view.AnimatorFactory;
	
	import core.interfaces.IDispose;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import sng.modules.view.components.city.logic.City;
	import sng.modules.view.components.city.logic.CityView;
	import sng.modules.view.components.city.logic.movement.*;
	
	//Create By royjin 20110706
	//可以移动的人物,汽车等基类
	//形象动画资源这快以后建议和建筑类整合
	public class MoveChar extends Tile implements IDispose
	{
		/*******************自有属性*******************/
		
		public var animaState:int = 0;						//动画状态
		protected var skin:AnimatorCollection;					//动画资源对象
		
		protected var _faceDirection:String = Directions.N;	//朝向
		protected var _isIdle:Boolean;							//是否可以执行新的指令
		
		private var _moveBehavior:TileMovement;				//移动逻辑
		
		public function get moveBehavior():TileMovement
		{
			return _moveBehavior;
		}
		public function set moveBehavior(value:TileMovement):void
		{
			_moveBehavior = value;
			_moveBehavior.tile = this;
			this.interceptedEventTypes = [];
		}
		
		public function MoveChar(cv:CityView)
		{
			this.cityView = cv;
			moveBehavior = new BasicMove();
		}
		//加载动画资源形象
		public function LoadSkin(path:String):void
		{
			skin = AnimatorFactory.CreateAnimatorCollection();
			skin.addEventListener(flash.events.Event.COMPLETE,OnLoadComplete,false,0,true);
			skin.Load(path);
		}
		protected function OnLoadComplete(event:Event):void
		{
			skin.removeEventListener(flash.events.Event.COMPLETE,OnLoadComplete);
			//临时兼容用
//			skin.scaleX = 0.5;
//			skin.scaleY = 0.5;
			//播放初始动画
			this.UpdateAnimator();
			//添加到引擎显示对象数组	
			this.sprites.push(skin);
			//抛处加载完成事件
			this.dispatchEvent(new Event(Event.COMPLETE));
			//Trace
			//trace("动画加载成功");
		}
		public function UpdateAnimator():void
		{
			skin.Play(this.animaState*4+Directions.DirectionToInt(this._faceDirection));
		}
		override public function dispose():void
		{
			for each(var i:AnimatorCollection in this.spritesArray)
			{
				i.Dps();
			}
			while(this.spritesArray.length>0)
			{
				this.spritesArray.pop();
			}
			super.dispose();
		}
		
		
		//移动到制定目标
		override public function moveTo(x:Number, y:Number, z:Number):void
		{
			_moveBehavior.execute([x, y, z]);
		}
		//传送到指定目标
		override public function spawnAt(col:Number, row:Number, _z:Number = 0):void
		{
			var b:TileMovement = _moveBehavior;
			moveBehavior = new BasicMove();
			moveBehavior.execute([col, row, _z]);
			moveBehavior = b;
		}
		public function get isIdle():Boolean
		{
			return _isIdle;
		}
		
		public function set isIdle(value:Boolean):void
		{
			
			_isIdle = value;
			if (_isIdle)
			{
				this.animaState = 0;
				this.UpdateAnimator();
			}
			
		}
		
		public function get faceDirection():String
		{
			return this._faceDirection;
		}
		
		public function set faceDirection(value:String):void
		{
			//trace(value);
			if (value == _faceDirection)
			{
				return;
			}
			_faceDirection = value;
			this.UpdateAnimator();
		}
		
	
	}
}